package com.hyl.trigger.http;

import com.hyl.api.IDCCService;
import com.hyl.api.response.Response;
import com.hyl.types.enums.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RTopic;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author hyl
 * @description
 * @create 2025-06-19 17:33
 */
@Slf4j
@RestController
@CrossOrigin("*")
@RequestMapping("/api/v1/gbm/dcc/")
public class DCCController implements IDCCService {
    /**
     * 动态值变更
     * <p>
     * curl http://127.0.0.1:8091/api/v1/gbm/dcc/update_config?key=downgradeSwitch&value=1
     * curl http://127.0.0.1:8091/api/v1/gbm/dcc/update_config?key=cutRange&value=0
     */
    @Resource
    private RTopic docTopic;
    @RequestMapping(value = "update_config",method = RequestMethod.GET)
    @Override
    public Response<Boolean> updateConfig(@RequestParam  String key,@RequestParam String value) {

    try {
        log.info("DCC动态配置值变更 key：{} value:{}",key,value);
        docTopic.publish(key+","+value);
        return Response.<Boolean>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .build();


    }catch (Exception e){
        log.error("DCC 动态配置值变更失败 key:{} value:{}", key, value, e);
        return Response.<Boolean>builder()
                .code(ResponseCode.UN_ERROR.getCode())
                .info(ResponseCode.UN_ERROR.getInfo())
                .build();
    }

    }
}
